package com.fp.m7.server.services;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fp.m7.api.manager.AccountManager;
import com.fp.m7.api.manager.CommentManager;
import com.fp.m7.api.model.Account;
import com.fp.m7.api.model.Comment;
import com.fp.m7.server.auth.AuthenticationChecker;
import com.fp.m7.server.helpers.CommentDtoFactory;
import com.fp.m7.shared.Constants;
import com.fp.m7.shared.dto.CommentDto;
import com.fp.m7.shared.services.CommentService;
import com.fp.m7.shared.validator.CommentValidator;

/**
 * The server side implementation of the Comments RPC service.
 */
@Service("comments")
@Transactional
public class CommentServiceImpl implements CommentService {

    @Autowired
    private AuthenticationChecker authChecker;

    @Autowired
    private CommentManager commentManager;

    @Autowired
    private AccountManager accountManager;

    @Autowired
    private CommentDtoFactory commentDtoFactory;

    @Autowired
    private CommentValidator commentValidator;

    @Transactional(readOnly = true)
    @Override
    public List<CommentDto> blog(String nickname, int pageNumber) {
        Account user = accountManager.findByNickname(nickname);
        return doBlog(user, pageNumber);
    }

    @Transactional(readOnly = true)
    @Override
    public List<CommentDto> blog(String nickname, Date from, Date to) {
        Account user = accountManager.findByNickname(nickname);
        return doBlog(user, from, to);
    }

    @Secured({ "ROLE_USER" })
    @Transactional(readOnly = true)
    @Override
    public List<CommentDto> blog(int pageNumber) {
        Account account = authChecker.getLoggedAccount();
        return doBlog(account, pageNumber);
    }

    @Secured({ "ROLE_USER" })
    @Transactional(readOnly = true)
    @Override
    public List<CommentDto> blog(Date from, Date to) {
        Account account = authChecker.getLoggedAccount();
        return doBlog(account, from, to);
    }

    private List<CommentDto> doBlog(Account user, int pageNumber) {
        List<Comment> comments = commentManager.findUsersLatestComments(user, Constants.MAX_COMMENTS_PER_PAGE, pageNumber);
        return commentDtoFactory.build(comments);
    }

    private List<CommentDto> doBlog(Account user, Date from, Date to) {
        List<Comment> comments = commentManager.findUsersCommentsByDate(user, from, to);
        return commentDtoFactory.build(comments);
    }

    @Secured({ "ROLE_USER" })
    @Transactional(readOnly = true)
    @Override
    public List<CommentDto> timeline(int pageNumber) {
        Account account = authChecker.getLoggedAccount();
        List<Comment> comments = commentManager.findCommentFeedForUser(account, Constants.MAX_COMMENTS_PER_PAGE,
                pageNumber);

        List<CommentDto> commentDtos = commentDtoFactory.build(comments);
        return commentDtos;
    }

    @Secured({ "ROLE_USER" })
    @Override
    public Boolean postComment(String content) {
        Account account = authChecker.getLoggedAccount();
        if (commentValidator.validate(content).isOk()) {
        	commentManager.createNew(account, content);
            return true;
        }
        return false;
    }

    @Secured({ "ROLE_USER" })
    @Override
    public Boolean deleteComment(CommentDto commentDto) {
        Account authAccount = authChecker.getLoggedAccount();
        Comment comment = commentManager.findById(commentDto.getId());
        if (comment != null && comment.getAuthor() != null && comment.getAuthor().equals(authAccount)) {
            // double check that this comment was posted by the authenticated user
            commentManager.delete(comment);
            return true;
        }
        return false;
    }

    public void setAuthChecker(AuthenticationChecker authChecker) {
        this.authChecker = authChecker;
    }

    public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}
    
    public void setCommentManager(CommentManager commentManager) {
		this.commentManager = commentManager;
	}

    public void setCommentDtoFactory(CommentDtoFactory commentDtoFactory) {
        this.commentDtoFactory = commentDtoFactory;
    }
    
    public void setCommentValidator(CommentValidator commentValidator) {
        this.commentValidator = commentValidator;
    }

}
